var app = getApp();
var WxParse = require('../../untils/wxParse/wxParse.js')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    list: [
      { 'id': 0, 'hidden': true }
    ],
    mainInfo: {},
    applied: '',
    rowCount: '',
    mainInfoImg:''
  },
  // 方法一
  hiddenBtn: function (e) {
    var that = this;
    // 获取事件绑定的当前组件
    var index = e.currentTarget.dataset.index;
    // 获取list中hidden的值
    // 隐藏或显示内容
    that.data.list[index].hidden = !that.data.list[index].hidden;
    that.setData({
      list: that.data.list
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    var _this = this;
    var typeId = options.typeid;
    this.data.typeId = typeId
    wx.request({
      url: app.globalData.SERVICE.ACTIVITY.DETAILS,
      data: {
        'id': typeId,
        'part.member.id': app.globalData.userid
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      method: "GET",
      dataType: "json",
      success: function (res) {
        if (res.data.code == 200) {
          _this.setData({
            mainInfo: res.data.obj,
            mainInfoImg: app.globalData.picServer + res.data.obj.img
          });
          var article = res.data.obj.content;
          WxParse.wxParse('article', 'html', article, _this);
          console.log(res);
        }
      }
    });

    wx.request({
      url: app.globalData.SERVICE.ACTIVITY.APPLIEDS,
      data: {
        'blind.id': typeId,
        'type': 3
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      method: "GET",
      dataType: "json",
      success: function (res) {
        if (res.data.code == 200) {
          console.log(res);
          _this.setData({
            applied: res.data.pageitems,
            rowCount: res.data.rowcount,
          })
        }
      }
    });

    wx.setNavigationBarTitle({
      title: '活动详情',
    })
  },
  apply: function (e) {//报名
  var _this=this;
  var typeId = this.data.typeId;
    var userInfo = wx.getStorageSync("userInfo");
    if (userInfo.member.sfhmd == 1) {
      wx.showModal({
        title: '提示',
        content: '您不符合报名条件！',
      })
    } else if (userInfo.member.sfrz == 0){//需要认证
    wx.showModal({
      title: '提示',
      content: '您的会员信息尚未经过认证不能进行操作，请先去认证！',               cancelTextcancelText:'取消',
      confirmText:'去认证',
      success:function(res){
        if(res.confirm){
          wx.navigateTo({
            url: '../../pages/auth/auth'
          })
        }
      }
    })
    }else {
      wx.request({//获取用户意向信息
        url: app.globalData.SERVICE.MATES.VERIFYHD,
        data: {
          'id': app.globalData.userid,
        },
        header: {
          'content-type': 'application/json'
        },
        method: "GET",
        dataType: "json",
        success: function (res) {
          var resData = res.data;
          if (resData.code == 200) {
            if (resData.errmsg.indexOf("择偶") > 0) {
              // 择偶条件没有完善跳转到择偶条件
              app.confirm("提示", "为更好地为您提供服务，请在“个人编辑”中完善择偶条件。").then(function (value) {
                if (value) {
                  wx.navigateTo({
                    url: '../../pages/personalBaseInfoEdit/personalBaseInfoEdit'
                  })
                }
              });
            } else if (resData.errmsg.indexOf("基本信息") > 0) {
              app.confirm("提示", "为更好地为您提供服务，请在“个人编辑”中完善个人资料。").then(function (value) {
                if (value) {
                  wx.navigateTo({
                    url: '../../pages/personalBaseInfoEdit/personalBaseInfoEdit'
                  })
                }
              });
            } else if (resData.errmsg.indexOf("形象照") > 0) {
              app.confirm("提示", "为更好地为您提供服务，请在“个人”中上传形象照、身份证、毕业证。").then(function (value) {
                if (value) {
                  wx.navigateTo({
                    url: '../../pages/personalBaseInfoEdit/personalBaseInfoEdit'
                  })
                }
              });
            } else {
              wx.request({
                url: app.globalData.SERVICE.ACTIVITY.APPLY,
                data: {
                  'part.blind.id': typeId,
                  'part.member.id': app.globalData.userid
                },
                header: {
                  'content-type': 'application/json' // 默认值
                },
                method: "GET",
                dataType: "json",
                success: function (res) {
                  if (res.data.code == 200) {
                    wx.showToast({
                      title: '报名成功',
                      icon:'success',
                    });
                    let isPart = 'mainInfo.isPart';
                    _this.setData({
                      [isPart]:1
                    });
                  }else{
                    wx.showToast({
                      title: res.data.errmsg,
                    })
                  }
                }
              });
            }
          }
        }
      });
    }
  },
  cancelApply: function () {//活动取消
  var _this=this;
    wx.request({
      url: app.globalData.SERVICE.ACTIVITY.CANCELAPPLY,
      data: {
        'xgmember.id': app.globalData.userid,
        'blind.id': this.data.typeId
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      method: "GET",
      dataType: "json",
      success: function (res) {
        if (res.data.code == 200) {
          wx.showToast({
            title: '取消报名成功！',
          })
          let isPart = 'mainInfo.isPart';
          _this.setData({
            [isPart]: 0
          });
        }
      }
    });
  },
  activitySign: function (e) {//活动签到
  var _this=this;
    wx.navigateTo({
      url: '../../pages/dailyAttendance/dailyAttendance?id=' + _this.data.mainInfo.isPart,
    });
  },
})